package com.edu.exam.entity.assessment.technical;

import com.edu.exam.enums.assessment.technical.QualityFeedbackTargetModule;
import com.edu.exam.enums.assessment.technical.QualityFeedbackPriority;
import com.edu.exam.enums.assessment.technical.QualityFeedbackStatus;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.experimental.SuperBuilder;

import java.time.LocalDateTime;

/**
 * 质量反馈实体类
 * 所属模块：B-02 题库管理模块
 * 对应数据库表：quality_feedback
 *
 * 根据E-06-03质量监控子模块设计实现
 * 对应quality_feedbacks数据表，用于存储向E-03和E-04模块发送的质量反馈信息
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@EqualsAndHashCode(callSuper = false)
public class QualityFeedback {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "feedback_id", length = 32, unique = true, nullable = false)
    private String feedbackId;

    @Enumerated(EnumType.STRING)
    @Column(name = "target_module", length = 20, nullable = false)
    private QualityFeedbackTargetModule targetModule;

    @Column(name = "task_id", length = 32)
    private String taskId;

    @Column(name = "grader_id", length = 32)
    private String graderId;

    @Column(name = "feedback_type", length = 50, nullable = false)
    private String feedbackType;

    @Column(name = "feedback_content", columnDefinition = "JSON", nullable = false)
    private String feedbackContent;

    @Column(name = "recommendations", columnDefinition = "JSON")
    private String recommendations;

    @Enumerated(EnumType.STRING)
    @Column(name = "priority", length = 20, nullable = false)
    private QualityFeedbackPriority priority;

    @Enumerated(EnumType.STRING)
    @Column(name = "status", length = 20, nullable = false)
    private QualityFeedbackStatus status;

    @Column(name = "sent_by", length = 32, nullable = false)
    private String sentBy;

    @Column(name = "sent_at", nullable = false)
    private LocalDateTime sentAt;

    @Column(name = "acknowledged_by", length = 32)
    private String acknowledgedBy;

    @Column(name = "acknowledged_at")
    private LocalDateTime acknowledgedAt;

    @Column(name = "response_content", columnDefinition = "JSON")
    private String responseContent;

    // 扩展字段
    @Column(name = "feedback_category", length = 50)
    private String feedbackCategory; // 反馈分类

    @Column(name = "urgency_level", length = 20)
    private String urgencyLevel; // 紧急程度

    @Column(name = "impact_assessment", columnDefinition = "JSON")
    private String impactAssessment; // 影响评估

    @Column(name = "action_required")
    private Boolean actionRequired; // 是否需要行动

    @Column(name = "action_deadline")
    private LocalDateTime actionDeadline; // 行动截止时间

    @Column(name = "action_taken", columnDefinition = "JSON")
    private String actionTaken; // 已采取行动

    @Column(name = "action_completed_at")
    private LocalDateTime actionCompletedAt; // 行动完成时间

    @Column(name = "follow_up_required")
    private Boolean followUpRequired; // 是否需要跟进

    @Column(name = "follow_up_date")
    private LocalDateTime followUpDate; // 跟进日期

    @Column(name = "satisfaction_rating", precision = 2, scale = 1)
    private Double satisfactionRating; // 满意度评分

    @Column(name = "effectiveness_rating", precision = 2, scale = 1)
    private Double effectivenessRating; // 有效性评分

    @Column(name = "improvement_suggestions", columnDefinition = "JSON")
    private String improvementSuggestions; // 改进建议

    @Column(name = "lessons_learned", columnDefinition = "JSON")
    private String lessonsLearned; // 经验教训

    @Column(name = "archived")
    private Boolean archived; // 是否已归档

    @Column(name = "archive_date")
    private LocalDateTime archiveDate; // 归档日期

    @PrePersist
    protected void onCreate() {
        LocalDateTime now = LocalDateTime.now();
        this.sentAt = now;

        // 设置默认值
        if (this.status == null) {
            this.status = QualityFeedbackStatus.SENT;
        }
        if (this.priority == null) {
            this.priority = QualityFeedbackPriority.NORMAL;
        }
        if (this.actionRequired == null) {
            this.actionRequired = false;
        }
        if (this.followUpRequired == null) {
            this.followUpRequired = false;
        }
        if (this.archived == null) {
            this.archived = false;
        }
    }

    }
